<template>
  <div class="app-container">
    <el-row>
      <el-col>
        <el-select
          v-model="queryParams.year"
          placeholder="选择年份"
          @change="handleYearChange"
        >
          <el-option
            v-for="item in dict.type.biz_work_year_list"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="text-banner">
      <span>待审批培训记录</span>
      <el-button
        size="mini"
        type="primary"
        circle
        icon="el-icon-refresh"
        plain
        @click="handleRefresh"
      />
    </div>

    <div class="month-title">
      <div class="float-right mb-10">
        <el-button
          size="mini"
          type="primary"
          plain
          icon="el-icon-menu"
          :disabled="!selectionList.length"
          @click="handleBatchApproval('pass')"
        >
          批量通过
        </el-button>
        <el-button
          size="mini"
          type="danger"
          plain
          icon="el-icon-menu"
          :disabled="!selectionList.length"
          @click="handleBatchApproval('reject')"
        >
          批量退回
        </el-button>
      </div>
    </div>
    <div>
      <el-table
        :data="tableList"
        border
        style="width: 100%"
        v-loading="loading"
        @selection-change="handleSelectionChange($event, index)"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="日期" width="180" align="center">
        </el-table-column>

        <el-table-column
          prop="theme"
          label="培训主题"
          min-width="180"
          align="center"
        >
          <template slot-scope="scope">
            <div class="pre-wrap align-center">
              {{ scope.row.theme || "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="培训地点" align="center">
        </el-table-column>
        <el-table-column prop="civilServantName" label="参加干部" align="center"></el-table-column>
        <el-table-column prop="deptName" label="所属站办所" align="center"></el-table-column>
        <el-table-column prop="remarks" label="备注" align="center">
        </el-table-column>
        <el-table-column
          prop="name"
          label="操作"
          align="center"
          width="130"
          fixed="right"
        >
          <template slot-scope="scope">
            <ApprovalLog :biz-id="scope.row.id" placement="bottom-end">
              <el-button
                type="text"
                size="mini"
                @click.stop="handleApproval(scope.row, 'pass')"
                v-hasPermi="['approval:work-training:pass']"
              >
                通过
              </el-button>
            </ApprovalLog>
            <ApprovalLog :biz-id="scope.row.id" placement="bottom-end">
              <el-button
                type="text"
                size="mini"
                @click.stop="handleApproval(scope.row, 'reject')"
                class="color-danger ml-10"
                v-hasPermi="['approval:work-training:reject']"
              >
                退回
              </el-button>
            </ApprovalLog>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { approval, approvalList } from "@/api/work/work-training";
import ApprovalLog from "@/components/ApprovalLog";
export default {
  name: "approval-work-training",
  dicts: ["biz_work_year_list"],
  props: {},
  components: { ApprovalLog },
  data() {
    return {
      tableList: [],
      loading: false,
      queryParams: {
        year: "",
      },
      selectionList: [],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.handleYearChange(new Date().getFullYear());
  },
  methods: {
    // 校验表格是否禁选
    checkSelectable(raw, index) {
      if (raw.auditStatusDict !== "DSH") {
        return false;
      }
      return true;
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.selectionList = selection;
    },
    // 批量审批
    handleBatchApproval(operation) {
      let auditRemark = "同意";
      let auditStatusDict = "SHTG";
      if (operation === "reject") {
        auditRemark = "不同意";
        auditStatusDict = "SHBTG";
      }
      let params = this.selectionList.map((item) => {
        return { id: item.id, auditRemark, auditStatusDict };
      });
      let content = `您正在审批已选中的 ${this.selectionList.length} 条培训记录，请输入审批意见`;
      this.approvalPrompt(operation, content, auditRemark, params);
    },

    // 审批操作
    handleApproval(raw, operation) {
      let params = {
        id: raw.id,
        auditStatusDict: "SHTG",
        auditRemark: "同意",
      };
      if (operation === "reject") {
        params.auditStatusDict = "SHBTG";
        params.auditRemark = "不同意";
      }
      let content = `您正在审批 【${raw.date}】 培训记录，请输入审批意见`;
      this.approvalPrompt(operation, content, params.auditRemark, [params]);
    },
    // 审批窗口
    approvalPrompt(operation, content, inputValue, params) {
      this.$prompt(content, "审批提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPlaceholder: "输入审批意见",
        inputType: "textarea",
        inputPattern: /\S/,
        inputValue: inputValue,
        inputErrorMessage: "审批意见不能为空",
        beforeClose: (action, instance, done) => {
          params.forEach((item) => {
            item.auditRemark = instance.inputValue;
          });
          if (action === "confirm") {
            const loading = this.$loading({
              target: ".el-message-box",
              lock: true,
              text: "数据处理中",
              spinner: "el-icon-loading",
              background: "rgba(255,255,255,0.6)",
            });
            approval(params)
              .then(() => {
                loading.close();
                if (operation === "pass") {
                  this.$modal.msgSuccess("已通过");
                } else if (operation === "reject") {
                  this.$modal.msgError("已退回");
                }
                this.handleRefresh();
                done();
              })
              .catch(() => {
                loading.close();
                done();
              });
          } else {
            done();
          }
        },
      });
    },
    // 刷新
    handleRefresh() {
      this.getList();
    },
    // 校验按钮显示
    // 操作类型、审核状态
    checkBtnVisible(type, audit) {
      switch (type) {
        case "pass": // 通过
        case "reject": // 退回
          return audit === "DSH";
        case "finish": // 审核已完成
          return audit === "SHTG";
        default:
          return false;
      }
    },

    // 年度选择
    handleYearChange(year) {
      this.queryParams.year = year;
      this.getList();
    },

    getList() {
      this.loading = true;
      approvalList(this.queryParams)
        .then((res) => {
          this.loading = false;

          let { rows } = res;
          this.tableList = rows;
        })
        .catch((err) => (this.loading = false));
    },
  },
};
</script>

<style lang="scss" scoped>
@import url("~@/assets/styles/work.scss");
::v-deep .el-button--text [class*="el-icon-"] + span {
  margin-left: 0;
}
.collapse-title {
  font-size: 16px;
  font-weight: 600;
  padding-left: 20px;
}
</style>
